#include "stdafx.h"
#include "ConfigPage.h"
#include "ui_MainWindow.h"
#include "Config.h"

using namespace std;
using namespace AirCrash::Calypso;

ConfigPage::ConfigPage() : mUi(0) {
}

ConfigPage::~ConfigPage() {
}

void ConfigPage::keyPressEvent(QKeyEvent * e) {
}

void ConfigPage::paintEvent(QPaintEvent * e) {
}

void ConfigPage::showEvent(QShowEvent * e) {
	if (mUi) {
		mUi->mMenuBar->setVisible(true);
		mUi->mConfigMenu->setEnabled(true);

		//Set types database available
		mUi->mTypeDatabase->clear();
		QStringList drivers(QSqlDatabase::drivers());
		foreach(QString driverName, drivers) {
			if (driverName == "QSQLITE") {
				mUi->mTypeDatabase->addItem("SQLite", "QSQLITE");
			} else if (driverName == "QODBC") {
				mUi->mTypeDatabase->addItem("ODBC", "QODBC");
			} else if (driverName == "QMYSQL") {
				mUi->mTypeDatabase->addItem("MySql", "QMYSQL");
			} else if (driverName == "PSQL") {
				mUi->mTypeDatabase->addItem("PostgreSQL", "PSQL");
			}
		}
		

		mUi->mTypeDatabase->setCurrentIndex( mUi->mTypeDatabase->findText(Config::get("Database", "type").toString()) );
		mUi->mTypeOverlay->setCurrentIndex( mUi->mTypeOverlay->findText(Config::get("Image", "overlayType").toString()) );
		mUi->mColorOverlay->setCurrentIndex( mUi->mColorOverlay->findText(Config::get("Image", "overlayTextColor").toString()) );
		mUi->mTextOverlay->setText(Config::get("Image", "overlayText").toString());
		mUi->mImageOverlay->setText(Config::get("Image", "overlayImage").toString());
		mUi->mMinWaitTime->setText(Config::get("Settings", "waitTimeScannedCode").toString());
		mUi->mAllowNextDays->setChecked(Config::get("Settings", "allowNextDay").toBool());
		mUi->mFullScreen->setChecked(Config::get("Settings", "fullScreen").toBool());
		mUi->mAllowShutdown->setChecked(Config::get("Settings", "allowShutdown").toBool());
		mUi->mBackupImages->setChecked(Config::get("Settings", "backupImages").toBool());
		mUi->mHideMouse->setChecked(Config::get("Settings", "hideMouse").toBool());
		mUi->mWebcam->setChecked(Config::get("Settings", "webcamEnabled").toBool());
	}
}

void ConfigPage::hideEvent(QHideEvent * e) {
	if (mUi) {
		mUi->mConfigMenu->setEnabled(false);
	}
}

void ConfigPage::setUi(Ui::MainWindowClass * ui) {
	mUi = ui;
	connect( mUi->mConfigSave, SIGNAL(triggered()), this, SLOT(saveConfig()) );
}

void ConfigPage::saveConfig() {
	Config::set("Database", "type", mUi->mTypeDatabase->currentText());
	Config::set("Image", "overlayType", mUi->mTypeOverlay->currentText());
	Config::set("Image", "overlayTextColor", mUi->mColorOverlay->currentText());
	Config::set("Image", "overlayText", mUi->mTextOverlay->text());
	Config::set("Image", "overlayImage", mUi->mImageOverlay->text());
	Config::set("Settings", "waitTimeScannedCode", mUi->mMinWaitTime->text());
	Config::set("Settings", "allowNextDay", mUi->mAllowNextDays->isChecked());
	Config::set("Settings", "fullScreen", mUi->mFullScreen->isChecked());
	Config::set("Settings", "allowShutdown", mUi->mAllowShutdown->isChecked());
	Config::set("Settings", "backupImages", mUi->mBackupImages->isChecked());
	Config::set("Settings", "hideMouse", mUi->mHideMouse->isChecked());
	Config::set("Settings", "webcamEnabled", mUi->mWebcam->isChecked());

	if (Config::get("Settings", "hideMouse").toBool()) {
		mUi->mTombolaPage->setCursor( QCursor( Qt::BlankCursor ) );
	}
	if (Config::get("Settings", "webcamEnabled").toBool()) {
		mUi->mImages->setVisible(false);
		mUi->mOpenCvWidget->setVisible(true);
	} else {
		mUi->mImages->setVisible(true);
		mUi->mOpenCvWidget->setVisible(false);
	}

	Config::sync();

	QMessageBox::information(this, "Opgeslagen", "De configuratie is opgeslagen.");
}